package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    //新增
    @Override
    public void addMenu(SysMenu sysMenu) {

        //创建时间和更新时间
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setIsShow(1);
        //添加数据
        save(sysMenu);
    }

    //删除
    @Override
    public void deleteMenu(Integer id) {

        //删除时需要特别注意  如果在关系表表中有此id
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, SysRoleMenu::getMenuId, id);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.list(wrapper);

        if (sysRoleMenuList != null && sysRoleMenuList.size() != 0) {
            //说明集合不为空 有数据  需要先将数据删除
            sysRoleMenuService.remove(wrapper);
        }
        removeById(id);
    }


    //回显数据
    @Override
    public ResultData getInfo(Integer id) {
        SysMenu sysMenu = getById(id);
        return ResultData.ok("", sysMenu);
    }


    //修改
    @Override
    public void updateMenu(SysMenu sysMenu) {
        sysMenu.setUpdateDate(LocalDateTime.now());
        updateById(sysMenu);
    }


}
